﻿#region LicenseInfo
/*
 * WoW Automation
 * Copyright 2008, 2009
 * Michael Sutton
 * 
 * This file is part of Wow Automation.
 * 
 * Wow Automation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Wow Automation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Wow Automation.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using AForge.Imaging.Filters;

namespace FishBotConcept
{
    public partial class MainApplicationForm : Form
    {
        public MainApplicationForm()
        {
            InitializeComponent();
            Maintimer.Tick += Maintimer_Tick;
            CastTimer.Tick += CastTimer_Tick;
            sensitivity = 6;
            _thresholdFilter = 180;
        }
        private static byte _thresholdFilter { get; set; }
        private static Threshold thresFilter = new Threshold(0);
        private static IFilter grayscaleFilter = new GrayscaleRMY();
        private static Bitmap processImage(Bitmap bmp)
        {
            // get grayscale image
            
            bmp = grayscaleFilter.Apply(bmp);
            // apply threshold filter
            
            thresFilter.ThresholdValue = _thresholdFilter;
            bmp = thresFilter.Apply(bmp);
            // now have highlighted the splash



            return bmp;
        }
        private static AForge.Imaging.SusanCornersDetector mcd = new AForge.Imaging.SusanCornersDetector();
        private static Point[] HighLightCoords(Bitmap bmp)
        {
        
            mcd.GeometricalThreshold = sensitivity;
            Point[] lp = mcd.ProcessImage(bmp);
            return lp;
        }
      
        public void MouseClicker(int x, int y)
        {
            IntPtr hwnd = Win32Support.FindWindow("WoW", "World of Warcraft");
            Win32Support.RECT myRect = new Win32Support.RECT();
            Win32Support.GetWindowRect(hwnd, ref myRect);

            int dest_x = (myRect.left) + x;
            int dest_y = ((myRect.top) + y);


            Win32Support.SetCursorPos(dest_x, dest_y);
            System.Threading.Thread.Sleep(50);
            Win32Support.PostMessage(hwnd, WindowsMessageCode.WM_RBUTTONDOWN, (IntPtr)0, (IntPtr)0);
            System.Threading.Thread.Sleep(50);
            System.Threading.Thread.Sleep(50);
            Win32Support.PostMessage(hwnd, WindowsMessageCode.WM_RBUTTONUP, (IntPtr)0, (IntPtr)0);


        }
        public void SendKey(Keys key, int timeMilliSeconds)
        {
            IntPtr hwnd = Win32Support.FindWindow("WoW", "World of Warcraft");

            IntPtr keytosend = (IntPtr)key;
            Win32Support.PostMessage(hwnd, WindowsMessageCode.WM_KEYDOWN, keytosend, IntPtr.Zero);
            for (int i = 0; i <= timeMilliSeconds; i++)
            {
                System.Threading.Thread.Sleep(1);

            }

            Win32Support.PostMessage(hwnd, WindowsMessageCode.WM_KEYUP, keytosend, IntPtr.Zero);
        }
        private void castRod()
        {
            SendKey(Keys.D1, 50);
        }

        private void clickThePoints(ICollection<Point> items)
        {
            int xtot = 0;
            int ytot = 0;

            foreach (Point pnt in items)
            {
                xtot += pnt.X;
                ytot += pnt.Y;
            }
            xtot = xtot / items.Count;
            ytot = ytot / items.Count;
            MouseClicker(xtot + 8, ytot + 20);
            MouseClicker(20, 20);

            System.Threading.Thread.Sleep(1500);
            

        }

        private readonly Timer Maintimer = new Timer();
        private readonly Timer CastTimer = new Timer();
        Bitmap bmp = null;
        void Maintimer_Tick(object sender, EventArgs e)
        {
            Maintimer.Enabled = false;
            IntPtr hwnd = Win32Support.FindWindow("WoW", "World of Warcraft");
            
            if (!hwnd.Equals(IntPtr.Zero))
            {
                Win32Support.RECT myRect = new Win32Support.RECT();
                Win32Support.GetWindowRect(hwnd, ref myRect);
                int winFrameWidth = Win32Support.GetSystemMetrics(Win32Support.SM_CXSIZEFRAME);
                int winCaptionHeight = Win32Support.GetSystemMetrics(Win32Support.SM_CYCAPTION);
                //adjust for just the size of the window
                myRect.top += winCaptionHeight + winFrameWidth;
                myRect.bottom -= winFrameWidth;
                myRect.left += winFrameWidth;
                myRect.right -= winFrameWidth;

                //only retrieve the part of the screen we need
                bmp = Win32Support.GetScreenImage(Win32Support.GetDesktopWindow(), myRect);
            }
            Bitmap bmpOrg = bmp;
         
            bmp = processImage(bmp);

            Application.DoEvents();
            Point[] p = HighLightCoords(bmp);
            if (p.Length > 0)
            {
                CastTimer.Enabled = false;
                listBox1.Items.Clear();

                foreach (Point pp in p)
                {
                    listBox1.Items.Add(string.Format("{0},{1}", pp.X, pp.Y));
                    break;
                }

              //  pictureBox1.BackgroundImage = bmpOrg;
                
                clickThePoints(p);
                castRod();
                CastTimer.Enabled = true;
            }
            pictureBox1.BackgroundImage = bmpOrg;//bmpOrg;
            pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
            Maintimer.Enabled = true;
   
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Maintimer.Enabled = !Maintimer.Enabled;
            Maintimer.Interval = 10;
            CastTimer.Interval = 20000;
            CastTimer.Enabled = Maintimer.Enabled;
            castRod();
                
        }

        void CastTimer_Tick(object sender, EventArgs e)
        {
            castRod();
        }

        private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(domainUpDown1.Text))
            {
                sensitivity = int.Parse(domainUpDown1.Text);
            }
            else
            {
                sensitivity = 6;
            }
        }
        private static int sensitivity { get; set; }

        private void domainUpDown2_SelectedItemChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(domainUpDown2.Text))
            {
                _thresholdFilter = byte.Parse(domainUpDown2.Text);
            }
            else
            {
                _thresholdFilter = 180;
            }
        }
    }
}
